package com.lbd.gulimall.product.web;

import com.lbd.gulimall.product.entity.CategoryEntity;
import com.lbd.gulimall.product.service.CategoryService;
import com.lbd.gulimall.product.vo.Catalog2VO;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: linbd
 * @Description:
 * @Date: 2023/2/11 23:33
 */
@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping({"/", "/index.html"})
    public String toIndex(Model model) {
        List<CategoryEntity> categoryEntities = categoryService.getLevel1();
        //页面加载时查询所有的一级分类信息，放到request域中
        model.addAttribute("categoryEntities", categoryEntities);
        return "index";
    }

    @ResponseBody
    @GetMapping("index/catalog.json")
    public Map<String, List<Catalog2VO>> getCategoryInfo() {
        Map<String, List<Catalog2VO>> catalogMap = categoryService.getCategoryInfo();
        return catalogMap;
    }

    @ResponseBody
    @GetMapping("testCacheable")
    public String getCached() {
        categoryService.getCached();
        return "OK";
    }


    @GetMapping("/read")
    @ResponseBody
    public String read() {
        RReadWriteLock lock = redissonClient.getReadWriteLock("ReadWrite-Lock");
        RLock rLock = lock.readLock();
        String s = "";
        try {
            rLock.lock();
            System.out.println("读锁加锁" + Thread.currentThread().getId());
            Thread.sleep(5000);
            s = stringRedisTemplate.opsForValue().get("lock-value");
        } finally {
            rLock.unlock();
            return "读取完成:" + s;
        }
    }

    @GetMapping("/write")
    @ResponseBody
    public String write() {
        RReadWriteLock lock = redissonClient.getReadWriteLock("ReadWrite-Lock");
        RLock wLock = lock.writeLock();
        String s = UUID.randomUUID().toString();
        try {
            wLock.lock();
            System.out.println("写锁加锁" + Thread.currentThread().getId());
            Thread.sleep(10000);
            stringRedisTemplate.opsForValue().set("lock-value", s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            wLock.unlock();
            return "写入完成:" + s;
        }
    }

    @GetMapping("/park")
    @ResponseBody
    public String park() {
        //可用于分布式限流。tryAcquire == 当信号量为0了，立即返回false，告诉客户端，访问失败，而不用一直阻塞式的等待
        RSemaphore park = redissonClient.getSemaphore("park");
        try {
            park.acquire(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "停进2";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go() {
        RSemaphore park = redissonClient.getSemaphore("park");
        park.release(2);
        return "开走2";
    }

    @GetMapping("/setLatch")
    @ResponseBody
    public String setLatch() {
        RCountDownLatch latch = redissonClient.getCountDownLatch("CountDownLatch");
        try {
            latch.trySetCount(5);
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "门栓被放开";
    }

    @GetMapping("/offLatch")
    @ResponseBody
    public String offLatch() {
        RCountDownLatch latch = redissonClient.getCountDownLatch("CountDownLatch");
        latch.countDown();
        return "门栓被放开1";
    }
}
